BestProfilesPortlet = Class.extend(
		{
			init : function() {
				this.name = "BestProfilesPortlet";
				this.model = {};
				this.defaultResultNo = 5;
				this.registerObserver();
			},
			
			onBegin: function(){
				this.cache = new Array();
				this.expertiseID = this.getRequest().getParam('id');
			},
			
			// done
			run : function() {
				_this = this;
				this.getPortletPlaceholder().paintCanvas(this.render());
				this.onAjax('ajax', 'get-expert-rank-topic', {'cid':this.expertiseID}, 'GET', {
						'onSuccess': function(ret)	{
							_this.cache["bestprofilepage"] = 0;
							_this.cache["bestprofiles"] = new Array();
							if(ret.length != 0){
								_this.cache["bestprofiles"] = ret;
								_this.fetchData(ret, 0, _this.defaultResultNo);
							} else {
								$(".top-userpro").append("Chưa có xếp hạng");
							}
						},
						'onFailure': function(message)	{
						}
				}, true, 300000);
			},
			
			fetchData: function(ret,start,stop){
				$(".top-userpro").empty();
				var isRanked = true;
				for( var i = start; i< stop; i++){
					if(ret[i] == undefined){
						continue;
					}
					var objParam = {};
					objParam.avatar = ret[i].avatar ;
					objParam.name = ret[i].name ;
					objParam.topic = ret[i].catchWord ;
					if(ret[i].rank != undefined){
						objParam.rank = ret[i].rank;
						isRanked = false;
					} else {
						objParam.rank = (i+1);
					}
					objParam.id = ret[i].id;
					var obj = {};
					obj.er = Math.round(ret[i].expertRank * 10000)/100;
					obj.total = 1000;
					objParam.tips = tmpl("ExpertiseRankDescription",obj);
					var data = tmpl("BestProfilesPortletItem",objParam);
					$(".top-userpro").append(data);
				}
				if(!isRanked){
					$(".top-userpro").prepend("<div>Chưa có xếp hạng</div><br/>");
				}
				var sbj = SingletonFactory.getInstance(Subject);
				sbj.notifyEvent("HtmlUpdated");
				
			},
			
			onBestProfilesNext: function(){
				this.cache["bestprofilepage"] ++;
				if(this.cache["bestprofilepage"] * this.defaultResultNo >= this.cache["bestprofiles"].length){
					this.cache["bestprofilepage"] --;
					return;
				}
				var start = this.cache["bestprofilepage"] * this.defaultResultNo;
				var stop = this.cache["bestprofilepage"] * this.defaultResultNo + this.defaultResultNo;
				this.fetchData(this.cache["bestprofiles"], start, stop);
			},
			
			onBestProfilesPrevious: function(){
				if(this.cache["bestprofilepage"] == 0){
					return;
				}
				this.cache["bestprofilepage"] --;
				var start = this.cache["bestprofilepage"] * this.defaultResultNo;
				var stop = this.cache["bestprofilepage"] * this.defaultResultNo + this.defaultResultNo;
				this.fetchData(this.cache["bestprofiles"], start, stop);
			},
			
			onReloadPage: function(){
				this.onBegin();
				this.run();
			},
			
			onEnd: function()	{
				this.unregisterObserver();
			}
		}).implement(PortletInterface).implement(RenderInterface).implement(ObserverInterface).implement(AjaxInterface);